package cn.buk.tms.entity.misc;


import cn.buk.common.util.DateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
import jakarta.persistence.Transient;
import jakarta.persistence.UniqueConstraint;

/**
 * @author yfdai
 */
@Entity
@Table(name = "t101", uniqueConstraints = @UniqueConstraint(name = "uk_user", columnNames = {
    "username"}))
public class User {

  public static final String DEMO_USERNAME = "demo";

  /**
   * 用户手机未验证
   */
  public static final String USER_MOBILE_NOT_VERIFIED = "0";

  /**
   * 用户手机已经验证过
   */
  public static final String USER_MOBILE_VERIFIED = "1";

  /**
   * 用户使用手机接收密码注册，登录即可将手机验证状态修改为已验证。
   */
  public static final String USER_MOBILE_REGISTERED = "2";

  /**
   * 普通用户，散客
   */
  public static final int USER_TYPE_PSG = 0;

  /**
   * 企业用户
   */
  public static final int USER_TYPE_ENT = 1;

  /**
   * 正常状态
   */
  public static final int USER_STATUS_NORMAL = 1;

  /**
   * 停用状态
   */
  public static final int USER_STATUS_BANNED = 4;

  public static final String DEFAULT_PASSWORD = "123abc";

  /**
   * 查找员工信息（用户）
   */
  public static final int SEARCH_MODE_EMPLOYEE = 0;

  /**
   * 查找员工信息（会员）
   */
  public static final int SEARCH_MODE_MEMBER = 1;

  @Id
  @Column(name = "usn")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private int id;

  @Column(length = 32, nullable = false, unique = true)
  private String username = "";


  /**
   * 密码
   */
  @Deprecated
  @Column(name = "password1", length = 32)
  private String password;

  /**
   * 密码加密后的字符串
   */
  @Column(name = "md5_password", length = 32)
  private String md5Password;

  /**
   * 密码是否锁定，为1表示锁定，不允许修改
   */
  @Column(name = "password_locked")
  private Integer password_locked;

  @ManyToOne
  @JoinColumn(name = "enterprise_id", foreignKey = @ForeignKey(name = "fk_user_enterprise"))
  private Enterprise enterprise;

  @Column(name = "enterprise_id", insertable = false, updatable = false)
  private Integer enterpriseId;

  /**
   * 上级企业ID，属于哪个差旅公司
   */
  @Column(name = "parent_enterprise")
  private Integer parentEnterprise;

  /**
   * 企业微信中成员UserId, 一般与username一样，但也会不一致
   */
  @Column(name = "ww_user_id", length = 64)
  private String wwUserId;

  /**
   * 全名
   */
  @Column(name = "empname", length = 20)
  private String empname = "";


  /**
   * 客户服务分组，对应GroupCustomerService.groupCode
   * @see GroupCustomerService
   */
  @Column(name = "group_code", length = 5)
  private String customerServiceGroupCode;

  /**
   * 账号状态 null, 1: 正常 0: 未启用 4: 禁用/停用
   */
  private Integer status;


  /**
   * 该用户使用黑屏时的用户名,多个使用逗号隔开
   */
  private String etermUsername;


  /**
   * 是否管理员 0-不是 1-是
   */
  @Column(columnDefinition = "CHAR(1)")
  private String isadmin = "0";


  @Column(length = 50)
  private String email = "";

  @Column(length = 1)
  private String emailVerified = "0";

  /**
   * 如果创建到企业微信，同一个企业内手机号是唯一的，不能重复
   */
  @Column(name = "mobilePhone", length = 15)
  private String mobileNo = "";

  /**
   * 0，null 表示未验证 2, 表示注册密码为短信发送，则登录就认为是手机验证过 1. 手机验证了
   */
  @Column(length = 1)
  private String mobilePhoneVerified = USER_MOBILE_NOT_VERIFIED;

  /**
   * 账号有效截止日期
   */
  @Column(name = "validDate")
  private Date expireDate = DateUtil.addDays(DateUtil.getCurDate(), 1000);

  /**
   * 登录次数
   */
  private int loginTimes;

  /**
   * 是否允许执行[机票预定]功能 1-允许 0-不允许 其它-不允许
   */
  @Column(length = 1)
  private String sdAllowed;

  @Transient
  private String passwordOld;

  @Transient
  private String passwordConfirm;

  private String birthday;

  private String phone;

  private String postcode;

  private String address;

  /**
   * 0 - 散客 1 - 企业客户
   */
  private int userType;

  @ManyToMany(fetch = FetchType.EAGER)
  @JoinTable(name = "user_dept",
      joinColumns = @JoinColumn(name = "user_id"),
      inverseJoinColumns = @JoinColumn(name = "ent_dept_id"))
  private List<EntDepartment> departments;

  /**
   * 企业客户的员工在系统中的用户信息，理论上是1对1
   */
  @Column(name = "passenger_id")
  private Integer passengerId;

  @Temporal(TemporalType.DATE)
  @Column(updatable = false, nullable = false)
  private Date createTime;

  @Temporal(TemporalType.TIMESTAMP)
  @Column(insertable = false)
  private Date lastUpdate;

  /**
   * BukPM对应的用户属性
   */

  @Column(length = 1)
  private String isStatic = "0";
  @Column(length = 1)
  private String isDisable = "0";
  @Column(length = 1)
  private String isDistrust = "0";

  @Column(name = "level", length = 3)
  private String cmdLevel = "0";
  @Column(length = 3)
  private String groupIdx = "0";
  @Column(length = 3)
  private String lockLevel = "0";

  @Column(length = 15)
  private String allowFrom;
  @Column(length = 6)
  private String officeNo;
  @Column(length = 80)
  private String cmdTag;
  @Column(length = 80)
  private String memo;

  @Column(length = 20)
  private String printerAllowed;

  @Column(length = 1)
  private String multiLogin = "0";
  @Column(length = 1)
  private String xeOwnPnr = "0";
  @Column(length = 1)
  private String doLog = "1";
  @Column(length = 1)
  private String sdIntl = "1";

  private int dzQuota;
  @Column(insertable = false, updatable = false)
  private Integer dzIssued = 0;

  private int avMax;
  @Column(insertable = false, updatable = false)
  private Integer avUsed = 0;


  /**
   * 记录与放大软件的同步状态 0 - 需要同步 1 - 已经同步
   */
  @Column(name = "pm_sync_status", insertable = false, updatable = false)
  private Integer pmSyncStatus;

  /**
   * 同步时间
   */
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "pm_sync_time", insertable = false, updatable = false)
  private Date pmSyncTime;

  /**
   * 职位
   */
  private String position;

  /**
   * 性别 0-未设置 1-男 2-女
   */
  private int gender;

  /**
   * 头像
   */
  private String avatar;

  /**
   * 别名
   */
  private String alias;

  /**
   * 对外展示的职位
   */
  @Column(name = "external_position")
  private String externalPosition;

  private String telephone;



  /**
   * 生成临时用户名
   */
  public static String getTemporaryUsername() {
    return "user_" + DateUtil.getCurDateTime().getTime();
  }

  public static boolean testTemporaryUsername(String username) {
    String regEx = "user_[0-9]+";
    return Pattern.compile(regEx).matcher(username).find();
  }


  /**
   * @return the username
   */
  public String getUsername() {
    if (username == null) {
      return "";
    }
    return username.trim();
  }

  /**
   * @param username the username to set
   */
  public void setUsername(String username) {
    this.username = username.trim();
  }


  /**
   * @return the empname
   */
  public String getEmpname() {
    if (this.empname == null) {
      return "";
    }

    return empname.trim();
  }

  /**
   * @param empname the empname to set
   */
  public void setEmpname(String empname) {
    this.empname = empname.trim();
  }

  /**
   * @return the id
   */
  public int getId() {
    return id;
  }

  /**
   * @param id the id to set
   */
  public void setId(int id) {
    this.id = id;
  }

  /**
   * @return the password
   */
  public String getPassword() {
    if (password == null) {
      return "";
    } else {
      return password.trim();
    }
  }


  /**
   * @return the isadmin
   */
  public String getIsadmin() {
    return isadmin == null ? "0" : isadmin;
  }

  /**
   * @param isadmin the isadmin to set
   */
  public void setIsadmin(String isadmin) {
    this.isadmin = isadmin;
  }

  /**
   * @return the expireDate
   */
  public Date getExpireDate() {
    return expireDate;
  }

  /**
   * @param expireDate the expireDate to set
   */
  public void setExpireDate(Date expireDate) {
    if (expireDate != null) {
      this.expireDate = expireDate;
    }
  }

  /**
   * @return the email
   */
  public String getEmail() {
    if (email == null) {
      return "";
    }

    return email.trim();
  }

  /**
   * @param email the email to set
   */
  public void setEmail(String email) {
    if (email == null) {
      this.email = "";
    } else {
      this.email = email.trim();
    }
  }


  /**
   * @return the mobileNo
   */
  public String getMobileNo() {
    if (mobileNo == null) {
      return "";
    }

    return mobileNo.trim();
  }


  /**
   * @param mobileNo the mobileNo to set
   */
  public void setMobileNo(String mobileNo) {
    this.mobileNo = mobileNo.trim();
  }


  /**
   * @return the emailVerified
   */
  public String getEmailVerified() {
    return emailVerified;
  }


  /**
   * @param emailVerified the emailVerified to set
   */
  public void setEmailVerified(String emailVerified) {
    this.emailVerified = emailVerified;
  }


  public String getMobilePhoneVerified() {
    if (mobilePhoneVerified == null) {
      return "0";
    } else {
      return mobilePhoneVerified;
    }
  }

  public void setMobilePhoneVerified(String mobilePhoneVerified) {
    this.mobilePhoneVerified = mobilePhoneVerified;
  }

  public int getLoginTimes() {
    return loginTimes;
  }

  public void setLoginTimes(int loginTimes) {
    this.loginTimes = loginTimes;
  }


  public Enterprise getEnterprise() {
    return enterprise;
  }

  public void setEnterprise(Enterprise enterprise) {
    this.enterprise = enterprise;
  }

  public String getPasswordOld() {
    return passwordOld;
  }

  public void setPasswordOld(String passwordOld) {
    this.passwordOld = passwordOld;
  }

  public String getPasswordConfirm() {
    if (this.passwordConfirm == null) {
      return "";
    } else {
      return passwordConfirm.trim();
    }
  }

  public void setPasswordConfirm(String passwordConfirm) {
    if (passwordConfirm == null) {
      this.passwordConfirm = "";
    } else {
      this.passwordConfirm = passwordConfirm.trim();
    }
  }

  public String getRealname() {
    if (empname == null) {
      return "";
    }
    return empname.trim();
  }

  public void setRealname(String realname) {
    this.empname = realname;
  }

  public String getBirthday() {
    return birthday;
  }

  public void setBirthday(String birthday) {
    this.birthday = birthday;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  public String getPostcode() {
    return postcode;
  }

  public void setPostcode(String postcode) {
    this.postcode = postcode;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public String getMobileVerified() {
    if (mobilePhoneVerified == null) {
      return "0";
    } else {
      return mobilePhoneVerified;
    }
  }

  public void setMobileVerified(String mobileVerified) {
    this.mobilePhoneVerified = mobileVerified;
  }

  public int getUserType() {
    return userType;
  }

  public void setUserType(int userType) {
    this.userType = userType;
  }

  public boolean isSdAllowed() {
    return this.sdAllowed != null && this.sdAllowed.compareTo("1") == 0;
  }

  public void setSdAllowed(boolean allowed) {
    this.sdAllowed = allowed ? "1" : "0";
  }

  public String getEtermUsername() {
    return etermUsername == null ? "" : etermUsername.trim().toUpperCase();
  }

  public void setEtermUsername(String etermUsername) {
    this.etermUsername = etermUsername;
  }

  public Date getLastUpdate() {
    return lastUpdate;
  }

  public void setLastUpdate(Date lastUpdate) {
    this.lastUpdate = lastUpdate;
  }

  public Date getCreateTime() {
    return createTime;
  }

  public void setCreateTime(Date createTime) {
    this.createTime = createTime;
  }

  public String getStatic() {
    return isStatic;
  }

  public void setStatic(String aStatic) {
    isStatic = aStatic;
  }

  public String getDisable() {
    return isDisable;
  }

  public void setDisable(String disable) {
    isDisable = disable;
  }

  public String getDistrust() {
    return isDistrust;
  }

  public void setDistrust(String distrust) {
    isDistrust = distrust;
  }

  public String getCmdLevel() {
    return cmdLevel;
  }

  public void setCmdLevel(String cmdLevel) {
    this.cmdLevel = cmdLevel;
  }

  public String getGroupIdx() {
    return groupIdx;
  }

  public void setGroupIdx(String groupIdx) {
    this.groupIdx = groupIdx;
  }

  public String getLockLevel() {
    return lockLevel;
  }

  public void setLockLevel(String lockLevel) {
    this.lockLevel = lockLevel;
  }

  public String getAllowFrom() {
    if (allowFrom != null) {
      allowFrom = allowFrom.trim();
    }
    return allowFrom;
  }

  public void setAllowFrom(String allowFrom) {
    this.allowFrom = allowFrom;
  }

  public String getOfficeNo() {
    if (officeNo == null) {
      officeNo = "*";
    } else {
      officeNo = officeNo.trim().toUpperCase();
    }
    return officeNo;
  }

  public void setOfficeNo(String officeNo) {
    this.officeNo = officeNo;
  }

  public String getCmdTag() {
    return cmdTag;
  }

  public void setCmdTag(String cmdTag) {
    this.cmdTag = cmdTag;
  }

  public String getMemo() {
    if (memo != null) {
      memo = memo.trim();
    }
    return memo;
  }

  public void setMemo(String memo) {
    this.memo = memo;
  }

  public String getPrinterAllowed() {
    if (printerAllowed != null) {
      printerAllowed = printerAllowed.trim();
    }
    return printerAllowed;
  }

  public void setPrinterAllowed(String printerAllowed) {
    this.printerAllowed = printerAllowed;
  }

  public String getMultiLogin() {
    return multiLogin;
  }

  public void setMultiLogin(String multiLogin) {
    this.multiLogin = multiLogin;
  }

  public String getXeOwnPnr() {
    return xeOwnPnr;
  }

  public void setXeOwnPnr(String xeOwnPnr) {
    this.xeOwnPnr = xeOwnPnr;
  }

  public String getDoLog() {
    return doLog;
  }

  public void setDoLog(String doLog) {
    this.doLog = doLog;
  }

  public String getSdIntl() {
    return sdIntl;
  }

  public void setSdIntl(String sdIntl) {
    this.sdIntl = sdIntl;
  }

  public int getDzQuota() {
    return dzQuota;
  }

  public void setDzQuota(int dzQuota) {
    this.dzQuota = dzQuota;
  }

  public int getDzIssued() {
    if (dzIssued == null) {
      return 0;
    }
    return dzIssued;
  }

  public void setDzIssued(int dzIssued) {
    this.dzIssued = dzIssued;
  }

  public int getAvMax() {
    return avMax;
  }

  public void setAvMax(int avMax) {
    this.avMax = avMax;
  }

  public int getAvUsed() {
    if (avUsed == null) {
      return 0;
    }
    return avUsed;
  }

  public void setAvUsed(int avUsed) {
    this.avUsed = avUsed;
  }

  public int getPmSyncStatus() {
    if (this.pmSyncStatus == null) {
      return 1;
    }
    return this.pmSyncStatus;
  }

  public void setPmSyncStatus(int pmSyncStatus) {
    this.pmSyncStatus = pmSyncStatus;
  }

  public Date getPmSyncTime() {
    return pmSyncTime;
  }

  public void setPmSyncTime(Date pmSyncTime) {
    this.pmSyncTime = pmSyncTime;
  }

  public void setParentEnterprise(int parentEnterprise) {
    this.parentEnterprise = parentEnterprise;
  }

  public int getParentEnterprise() {
    return parentEnterprise == null ? 0 : parentEnterprise;
  }

  public List<EntDepartment> getDepartments() {
    if (departments == null) {
      departments = new ArrayList<>();
    }
    return departments;
  }

  public void setDepartments(List<EntDepartment> departments) {
    this.departments = departments;
  }

  public String getWwUserId() {
    return wwUserId == null ? "" : wwUserId.trim();
  }

  public void setWwUserId(String wwUserId) {
    this.wwUserId = wwUserId;
  }

  public String getMd5Password() {
    return md5Password;
  }

  public void setMd5Password(String md5Password) {
    this.md5Password = md5Password;
  }

  public int getStatus() {
    return status == null ? 1 : status;
  }

  public void setStatus(Integer status) {
    this.status = status;
  }

  public int getEnterpriseId() {
    return enterpriseId == null ? 0 : enterpriseId;
  }

  public void setPosition(String position) {
    this.position = position;
  }

  public String getPosition() {
    return position;
  }

  public void setGender(int gender) {
    this.gender = gender;
  }

  public int getGender() {
    return gender;
  }

  public void setAvatar(String avatar) {
    this.avatar = avatar;
  }

  public String getAvatar() {
    return avatar;
  }

  public void setAlias(String alias) {
    this.alias = alias;
  }

  public String getAlias() {
    return alias;
  }

  public void setExternalPosition(String externalPosition) {
    this.externalPosition = externalPosition;
  }

  public String getExternalPosition() {
    return externalPosition;
  }

  public void setTelephone(String telephone) {
    this.telephone = telephone;
  }

  public String getTelephone() {
    return telephone;
  }

  public Integer getPassengerId() {
    return passengerId;
  }

  public void setPassengerId(Integer passengerId) {
    this.passengerId = passengerId;
  }

  public String getCustomerServiceGroupCode() {
    return customerServiceGroupCode;
  }

  public void setCustomerServiceGroupCode(String customerServiceGroupCode) {
    this.customerServiceGroupCode = customerServiceGroupCode;
  }

    public int getPassword_locked() {
        return password_locked == null ? 0 : password_locked;
    }
}
